package org.example.controller;

import com.github.pagehelper.PageInfo;
import org.example.entity.ClassAnswer;
import org.example.service.ClassAnswerService;
import org.example.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName classAnswerContoller
 * @Description TODO
 * @Author qiuye
 * @Date 2025年08月19日 0:38
 * @Version 1.0
 */
@RestController
@RequestMapping("classAnswer")
public class ClassAnswerController {
    @Autowired
    private ClassAnswerService classAnswerService;
    @GetMapping("{id}")
    public R getAnswer(@PathVariable("id") int questionId) {
        List<ClassAnswer> classAnswerById = classAnswerService.getClassAnswerById(questionId);
        return R.ok(classAnswerById);
    }
    @GetMapping("hui")
    public R getHui(ClassAnswer classAnswer,
                    @RequestParam(required = false,defaultValue = "1") int page,
                    @RequestParam(required = false,defaultValue = "5")int size) {
        PageInfo<ClassAnswer> classAnswerHui = classAnswerService.getClassAnswerHui(classAnswer, page, size);
        return R.ok(classAnswerHui);
    }
    @PostMapping("/add")
    public R addAnswer(@RequestBody ClassAnswer answer) {
        try {
            classAnswerService.addAnswer(answer);
            // 调用 R 类的 ok 方法，返回自定义的成功响应
            return R.ok("回复成功");
        } catch (Exception e) {
            // 调用 R 类的 error 方法，返回自定义的错误响应
            return R.error("回复失败：" + e.getMessage());
        }
    }

}


